<?php
class Core_IO {
		/** Shell prompt to display to a user */
		public static $prompt =  "> ";
		
		/** Shall whitespace chars be stripped from the input or not */
		public static $trimEnabled = true;
		
		private static $io_handle;
		
//***************************************************************************************
		public static function readUserInput($prompt = "", $temporaryPrompt = false)
		{
			if ($prompt != "" && $temporaryPrompt == false)
				self::$prompt = $prompt;
			
			if (!self::$io_handle)
			{
				self::$io_handle = fopen ("php://stdin","r");
			}
			
			if ($temporaryPrompt)
			{
				echo $prompt;
			}
			else
			{
				echo self::$prompt;
			}			
			
			$userInput = fgets(self::$io_handle);
			
			if (self::$trimEnabled) 
			{				
				$userInput = trim($userInput);
			}
			
			return $userInput;
		}
		
//***************************************************************************************
/** should be called when the input is not needed anymore. Closes the io handle */
		public static function close()
		{
			fclose (self::$io_handle);
		}
		
//***************************************************************************************
/** 
 * Makes sure that a part of a path is surrounded with slashes
 * E.g. for input: "some/directory" returns: "/some/directory/"
 *		for input: "/some/directory" returns: "/some/directory/"
 *		for input: "/some/directory/" returns: "/some/directory/"
 */
		public static function makePathSection($path)
		{
			
			//TODO: Rewrite! The function must also remove the multiple / or \, like if $path= "///some/dir//"
			
			// crop '\' or '/' on the ends of the string
			/*
			if ($path[0] === '\\' || $path[0] === '/') $path[0]='';
			if ($path[ strlen($path)-1 ] === '\\' || $path[ strlen($path)-1 ] === '/') $path[ strlen($path)-1 ]='';
			
			$path = trim($path);
			
			$path = '/'.$path.'/';
			*/
			return $path;
		}
}